package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.TransactionCasesEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PublishAddDealedCasesActivityModel extends AndroidViewModel {
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> ldServiceCity = new MutableLiveData<>();
    public MutableLiveData<TransactionCasesEntity> ldSaveSuccess = new MutableLiveData<>();
    public MutableLiveData<String> ldUpdateSuccess = new MutableLiveData<>();

    public PublishAddDealedCasesActivityModel(@NonNull Application application) {
        super(application);
    }


    public void getAddressModel(Context context) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    MToast.showToast(context, "网络连接超时，请检查网络");
                    return;
                }
                if (response.body().getCode().equals("200"))
                    ldServiceCity.postValue(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }


    /**
     * 发布案例
     */
    public void saveTransactionCases(Context context, Map<String, Object> map) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().saveTransactionCases(map).enqueue(new Callback<BaseEntity<TransactionCasesEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TransactionCasesEntity>> call, Response<BaseEntity<TransactionCasesEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldSaveSuccess.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TransactionCasesEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 更新案例
     */
    public void updateTransactionCases(Context context, Map<String, Object> map) {
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);

        EnterpriseAPIManager.getInstance().getEnterpriseAPI().updateTransactionCases(map).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ldUpdateSuccess.postValue(response.body().getData());
                } else {
                    MToast.showToast(context, response.body().getMsg());

                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
